//
//  ECPalceHolderTextView.m
//  ECERP.CRM
//
//  Created by 刘思麟 on 2018/10/22.
//  Copyright © 2018年 Mac. All rights reserved.
//

#import "ECPalceHolderTextView.h"

@interface ECPalceHolderTextView()<UITextViewDelegate>

@property (nonatomic,strong) UITextView * placeHolderTV;

@property (nonatomic,strong) UILabel * charNumLB;

@end

@implementation ECPalceHolderTextView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setContentInset:(UIEdgeInsets)contentInset {
    [super setContentInset:contentInset];
    self.placeHolderTV.contentInset = contentInset;
}

- (void)setupUI {
    self.placeHolderTV = [UITextView new];
    self.placeHolderTV.editable = NO;
    self.placeHolderTV.textColor = ECCOLOR(0x999999);
    self.placeHolderTV.font = ECFONT(14*PX_RELATIVE375);
    [self addSubview:self.placeHolderTV];
    self.placeHolderTV.backgroundColor = [UIColor clearColor];
    [self.placeHolderTV addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [self.placeHolderTV scrollToTopAnimated:YES];
        [self becomeFirstResponder];
    }]];
    self.placeHolderTV.delegate = self;
    self.delegate = self;
    
    self.charNumLB = [UILabel new];
    self.charNumLB.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0];
    self.charNumLB.textColor = ECCOLOR(0x999999);
    self.charNumLB.font = ECFONT(14 * PX_RELATIVE375);
    self.charNumLB.textAlignment = NSTextAlignmentRight;
    [self addSubview:self.charNumLB];
    [self bringSubviewToFront:self.charNumLB];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.placeHolderTV.frame = self.bounds;
    self.charNumLB.frame = CGRectMake(12 * PX_RELATIVE375, self.height - 36 * PX_RELATIVE375, self.width - 36 * PX_RELATIVE375, 20 * PX_RELATIVE375);
}

- (void)setPlaceHolder:(NSString *)placeHolder {
    _placeHolder = placeHolder;
    self.placeHolderTV.text = _placeHolder;
    [self.placeHolderTV scrollToTopAnimated:NO];
}

- (void)setMaxCharNum:(NSInteger)maxCharNum {
    _maxCharNum = maxCharNum;
    self.charNumLB.text = [NSString stringWithFormat:@"0/%ld",_maxCharNum];
}
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    UIMenuController *menuController = [UIMenuController sharedMenuController];
    if (menuController) {
        [UIMenuController sharedMenuController].menuVisible = NO;
    }
    return NO;
}

#pragma mark UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
    if (textView.text.length < 1) {
        self.placeHolderTV.hidden = NO;
    }else {
        self.placeHolderTV.hidden = YES;
    }
    self.charNumLB.text = [NSString stringWithFormat:@"%ld/%ld",textView.text.length,self.maxCharNum];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if (textView.text.length >= self.maxCharNum && text.length>=1) {
        return NO;
    }
    return YES;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.placeHolderTV) {
        if (self.placeHolderTV.contentOffset.y >= 1) {
            self.placeHolderTV.backgroundColor = [UIColor whiteColor];
        }else {
            self.placeHolderTV.backgroundColor = [UIColor clearColor];
        }
    }
}

@end
